package com.heima.behavior.service.impl;

import com.heima.behavior.pojo.ApFollowBehavior;
import com.heima.behavior.service.ApFollowBehaviorService;
import com.heima.model.behavior.dto.FollowBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.threadUtil.ThreadLocalUtil;
import com.heima.utils.common.threadUtil.UserInfo;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ApFollowBehaviorServiceImpl implements ApFollowBehaviorService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private MongoTemplate mongoTemplate;
    @Override
    public ResponseResult follow(FollowBehaviorDto dto) {

        //1. 校验参数
        if (dto.getArticleId() == null || dto.getAuthorId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        UserInfo userInfo = ThreadLocalUtil.get();
        if (userInfo == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //判断用户是否已经互相关注
        String follow_hash_key="USER_FOLLOW_"+userInfo.getId();
        String fan_hash_key="USER_FANS_"+dto.getAuthorId();

        Boolean hasKey = stringRedisTemplate.opsForHash().hasKey(fan_hash_key, dto.getAuthorId().toString());
        //用户已关注--关注
//        if(hasKey&&dto.getOperation()==0){
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
//        }
        //用户已关注--取消关注
        if(hasKey&&dto.getOperation()==1){
            //删除缓存数据
            stringRedisTemplate.opsForHash().delete(follow_hash_key,dto.getAuthorId().toString());
            stringRedisTemplate.opsForHash().delete(fan_hash_key,userInfo.getId().toString());
            //删除mongoDb数据
            Query query = Query.query(Criteria.where("userId").is(userInfo.getId()).and("followId").is(dto.getAuthorId()));
            mongoTemplate.remove(query, ApFollowBehavior.class);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        //用户未关注
        if(!hasKey&&dto.getOperation()==0){
            stringRedisTemplate.opsForHash().put(follow_hash_key,dto.getAuthorId().toString(),"1");
            stringRedisTemplate.opsForHash().put(fan_hash_key,userInfo.getId().toString(),"1");
            //
            //2.3.2 保存关注数据到数据库
            ApFollowBehavior apFollowBehavior = new ApFollowBehavior();
            apFollowBehavior.setArticleId(dto.getArticleId());
            apFollowBehavior.setCreatedTime(new Date());
            apFollowBehavior.setFollowId(dto.getAuthorId());
            apFollowBehavior.setUserId(userInfo.getId().longValue());

            mongoTemplate.insert(apFollowBehavior);
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
